package com.jx.flowershopmanager.home.order;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.EditText;

import com.jx.flowershopmanager.App;
import com.jx.flowershopmanager.R;
import com.jx.flowershopmanager.base.BaseActivity;
import com.jx.flowershopmanager.widget.SubmitButton;

import org.w3c.dom.Text;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class AddEditOrderActivity extends BaseActivity {
    private EditText saleDate;
    private EditText txtFlowerId;
    private EditText txtCount;
    private EditText txtRemark;
    private EditText txtClient;
    private SubmitButton button;
    @Override
    protected int layoutId() {
        return R.layout.add_edit_order_activity;
    }

    @Override
    protected void initView() {
        saleDate = findViewById(R.id.sale_date);
        txtFlowerId = findViewById(R.id.txt_flower_id);
        txtCount = findViewById(R.id.txt_count);
        txtRemark = findViewById(R.id.txt_remark);
        txtClient = findViewById(R.id.txt_client);
        button = findViewById(R.id.btn_save);
    }

    @Override
    protected void initData() {
        button.setOnClickListener(v->{
            addOrder();
        });
    }

    private void addOrder(){
        SQLiteDatabase db = App.getDbHelp().getWritableDatabase();
        long timecurrentTimeMillis = System.currentTimeMillis();
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy年MM月dd日", Locale.getDefault());
        try {
            ContentValues values = new ContentValues();
            values.put("date",sdf.format(timecurrentTimeMillis));/// 时间
            values.put("flower",txtFlowerId.getText().toString());/// 鲜花Id
            values.put("count",txtCount.getText().toString());///数量
            values.put("total",200);///总价
            values.put("client",txtClient.getText().toString());///客户名称
            values.put("remark",txtRemark.getText().toString());///备注
            values.put("emp",App.getAppUser().getName());///员工
            values.put("createAt",sdf.format(timecurrentTimeMillis));
            values.put("updateAt",sdf.format(timecurrentTimeMillis));
            db.insert("SalesRecord",null,values);
            button.showSucceed();
            button.reset();
        }catch (Exception e) {
            e.printStackTrace();
            button.showError(2000);
        }
    }
}
